package com.example.questionnaire.controller;

import com.example.common.utils.PageUtils;
import com.example.common.utils.R;
import com.example.questionnaire.entity.ResultEntity;
import com.example.questionnaire.service.ResultService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * @author hsz
 * @email 1416446590@qq.com
 * @date 2022-10-29 11:20:56
 */
@CrossOrigin
@RestController
@RequestMapping("questionnaire/result")
public class ResultController {
    @Autowired
    private ResultService resultService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = resultService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @ApiOperation("查看答卷")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        ResultEntity result = resultService.getById(id);
        return R.ok().put("data", result);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody ResultEntity result) {
        resultService.save(result);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody ResultEntity result) {
        resultService.updateById(result);
        return R.ok().put("data",result);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        resultService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 收集答卷
     *
     * @param resultEntity
     * @return
     */
    @ApiOperation("收集答卷")
    @PostMapping("/collectResult/{id}")
    public R collectResult(@RequestBody ResultEntity resultEntity) {
        int result = resultService.collectResult(resultEntity);
        if (result == 1) {
            return R.error(1, "答卷人身份不符合要求!");
        }
        return R.ok();
    }

    @ApiOperation("提交答卷")
    @PostMapping("/submit")
    public R submit(@RequestBody ResultEntity resultEntity) {
        boolean flag = resultService.submit(resultEntity);
        if (flag) {
            return R.ok();
        } else {
            return R.error("问卷达到上限，无法提交");
        }
    }
}
